package cn.com.callback;
import groovy.json.*;
import groovy.swing.impl.DefaultAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.elite.EliteBean;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wisdge.dataservice.Result;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import cn.com.elite.Util;

/**
 *
 * @Title: apiHandleWO
 * @Description:深圳水务系统工单数据处理同步接口--外部接口
 * @author: chieh
 * @date 2019-12-17 02:28:00
 *
 */

public class apiHandleWO implements ServiceBackInvoke{
	private static final Log log = LogFactory.getLog(apiHandleWO.class);
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");

		String getMethod = request.getMethod();
		log.debug("getMethod:" + getMethod);

		String inData = "";
		String outData = "";
		String dataSourceID = "dataSource";

		BufferedReader iny = null;
		if (getMethod.equals("GET")) {
			inData = request.getParameter("data");
			log.debug("原始Get请求参数:" + inData);
		} else {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				inData += line;
			}
			log.debug("原始Post请求参数:" + inData);
		}

		try {
			// 调用处理信息
			JSONObject inObject = new JSONObject(inData);
			log.debug("处理前的Json：" + inObject.toString());

			String sys_source = GetJV(inObject, "sys_source");
			String sys_flag = GetJV(inObject, "sys_flag");
			String arr_data = GetJV(inObject, "data");

			log.debug("解析信息-sys_source：" + sys_source + " sys_flag：" + sys_flag + " arr_data：" + arr_data);

			if(sys_source=="JT") {
				//集团
				outData = JTHandle(eliteBean, sys_source, sys_flag, arr_data, dataSourceID);
			}
			if(sys_source=="GM") {
				//光明
				outData = GMHandle(eliteBean, sys_source, sys_flag, arr_data, dataSourceID);
			}
			if(sys_source=="BA") {
				//宝安
				outData = BAHandle(eliteBean, sys_source, sys_flag, arr_data, dataSourceID);
			}
			

		} catch (Exception e) {
			log.error("接口处理异常" + e.getMessage());
			// 失败返回
			outData = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):2\",\"data\":[]}";

		}

		log.debug("名单接入处理完成返回结果:" + outData);

		return outData;
	}

	// 集团工单处理
	public static String JTHandle(EliteBean eliteBean,String sys_source, String sys_flag, String inJson, String dataSourceID) {
		long startTime = System.currentTimeMillis();
		JSONArray arrJsonArray;
		Map<String, String> dealMap = new HashMap<String, String>();
		Map<String, String> dealMsgMap = new HashMap<String, String>();
		JSONArray resArray = new JSONArray();
		String reJson = null;
		try {
			arrJsonArray = new JSONArray(inJson);

			int arrInt = arrJsonArray.length();
			log.debug("接入List数量:" + arrInt);

			for (int i = 0; i < arrInt; i++) {

				log.debug("当前处理数组位置:" + (i+1));
				JSONObject arrCustJson = arrJsonArray.getJSONObject(i);

				//解析接口推送的处理结果信息
				String workorderstatus = GetJV(arrCustJson, "workorderstatus");
				String modifyby = GetJV(arrCustJson, "modifyby");
				String modifyname = GetJV(arrCustJson, "modifyname");
				String modifydate = GetJV(arrCustJson, "modifydate");
				String id = GetJV(arrCustJson, "id");
				String handlebusiness1 = GetJV(arrCustJson, "handlebusiness1");
				String handlebusiness2 = GetJV(arrCustJson, "handlebusiness2");
				String handlebusiness3 = GetJV(arrCustJson, "handlebusiness3");
				String leakagecondition = GetJV(arrCustJson, "leakagecondition");
				String handledescription = GetJV(arrCustJson, "handledescription");
				String remarks = GetJV(arrCustJson, "remarks");
				String failurecause = GetJV(arrCustJson, "failurecause");
				String starttime = GetJV(arrCustJson, "starttime");
				String finishtime = GetJV(arrCustJson, "finishtime");
				String workingday = GetJV(arrCustJson, "workingday");
				String handlerby = GetJV(arrCustJson, "handlerby");
				String handlerphone = GetJV(arrCustJson, "handlerphone");
				String responsibleperson = GetJV(arrCustJson, "responsibleperson");
				String arriveddate = GetJV(arrCustJson, "arriveddate");
				String firstarrivaldate = GetJV(arrCustJson, "firstarrivaldate");
				String checkgrade = GetJV(arrCustJson, "checkgrade");
				String registercode = GetJV(arrCustJson, "registercode");
				String requestdate = GetJV(arrCustJson, "requestdate");
				String doordate = GetJV(arrCustJson, "doordate");
								
				//回访信息
				String userreflect = GetJV(arrCustJson, "userreflect");
				String serviceattitude = GetJV(arrCustJson, "serviceattitude");
				String quality = GetJV(arrCustJson, "quality");
				String returnpeople = GetJV(arrCustJson, "returnpeople");
				String returnname = GetJV(arrCustJson, "returnname");
				String feedbacktime = GetJV(arrCustJson, "feedbacktime");
				String satisfaction = GetJV(arrCustJson, "satisfaction");
				String empattire = GetJV(arrCustJson, "empattire");
				String feedback = GetJV(arrCustJson, "feedback");
				
				//状态
				String handlingopinions = GetJV(arrCustJson, "handlingopinions");
				String stepnode = GetJV(arrCustJson, "stepnode");
				String timelimit = GetJV(arrCustJson, "timelimit");
				
				String handlername = GetJV(arrCustJson, "handlername");
				String responsiblename = GetJV(arrCustJson, "responsiblename");
				String registercodename = GetJV(arrCustJson, "registercodename");

				String dealResult = "";
				String dealMessage = "";

				//处理步骤校验
				if(sys_flag=='4'&&(isNull(modifyby)||isNull(modifyname)||isNull(modifydate)||isNull(id)||isNull(starttime)||isNull(finishtime)))
				{
					dealResult = "0";
					dealMessage = isNullMSG("modifyby", modifyby) + isNullMSG("modifyname", modifyname) + isNullMSG("modifydate", modifydate) + isNullMSG("id", id) + isNullMSG("starttime", starttime) + isNullMSG("finishtime", finishtime);
					log.warn("调用参数非空校验异常:" + dealMessage);

				}
				//除处理步骤，其他状态的校验
				if(sys_flag!='4'&&(isNull(modifyby)||isNull(modifyname)||isNull(modifydate)||isNull(id)))
				{
					dealResult = "0";
					dealMessage = isNullMSG("modifyby", modifyby) + isNullMSG("modifyname", modifyname) + isNullMSG("modifydate", modifydate) + isNullMSG("id", id);
					log.warn("调用参数非空校验异常:" + dealMessage);

				}
				//申请延时步骤校验
				if(sys_flag=='9'&&(isNull(modifyby)||isNull(modifyname)||isNull(modifydate)||isNull(id)||isNull(timelimit)))
				{
					dealResult = "0";
					dealMessage = isNullMSG("modifyby", modifyby) + isNullMSG("modifyname", modifyname) + isNullMSG("modifydate", modifydate) + isNullMSG("id", id)+ isNullMSG("timelimit", timelimit);
					log.warn("调用参数非空校验异常:" + dealMessage);

				}
				else {
					//查询现有当前工单编号是否存在现有工单
					def getSelectSQL = eliteBean.chaxun_sql("Select R1.Relation_ID, R3.Handlegroup_ID, R3.Handleby_ID, R3.CurstepStatus,R2.curstep_id_map as Curstep_Id	,R1.receiver as return_area,R2.workorderid as workorderid From Tabcswgdmerge R1 Left Join Objective R2 On R1.Relation_Id = R2.Objective_Guid Left Join Elitetask R3 On R1.Relation_Id = R3.Objective_Guid Where case when R1.uciswordid is not null or R1.uciswordid!='' then R1.uciswordid else R1.workorderid end = '" + id + "'", dataSourceID);
					log.debug("查询当前同步的工单是否已经存在 SQL:" + getSelectSQL + " 结果数量:" + getSelectSQL.size());

					//获取当前工单编号对应的CRM唯一工单Guid Relation_ID
					String old_Relation_ID = null;
					String old_Handlegroup_ID = null;
					String old_Handleby_ID = null;
					String old_CurstepStatus = null;
					String old_Curstep_Id = null;
					String workorderid = null;
					//回访人员区域标识
					String return_area = null;

					if(getSelectSQL.size() > 0) {
						old_Relation_ID = getSelectSQL.get(0).get("Relation_ID");
						old_Handlegroup_ID = getSelectSQL.get(0).get("Handlegroup_ID");
						old_Handleby_ID = getSelectSQL.get(0).get("Handleby_ID");
						old_CurstepStatus = getSelectSQL.get(0).get("CurstepStatus");
						old_Curstep_Id = getSelectSQL.get(0).get("Curstep_Id");
						workorderid = getSelectSQL.get(0).get("workorderid");
						return_area = getSelectSQL.get(0).get("return_area");
					}else{
						old_Relation_ID = "";
						old_Handlegroup_ID = "";
						old_Handleby_ID = "";
						old_CurstepStatus = "";
						old_Curstep_Id = "";
						workorderid = "";
						return_area = "";
					}
					log.debug("查询现有当前工单编号是否存在现有工单-结果old_Relation_ID:" + old_Relation_ID + " old_Handlegroup_ID:" + old_Handlegroup_ID + " old_Handleby_ID:" + old_Handleby_ID + " old_CurstepStatus:" + old_CurstepStatus + " old_Curstep_Id:" + old_Curstep_Id+" workorderid:" + workorderid);

					// 提前获取工单的上一步处理人
					def getOldSelectSQL = eliteBean.chaxun_sql("Select R1.Handlegroup, R1.Handleby, R1.From_Stepidmap From Workorderpath3 R1 Left Join (Select Workorder_Guid, Max(Modifieddate) Modifieddate From Workorderpath3 Where To_Stepidmap = 'ZUOC2L' And Workorder_Guid = '" + old_Relation_ID + "' Group By Workorder_Guid) R2 On R1.Workorder_Guid = R2.Workorder_Guid And R1.Modifieddate = R2.Modifieddate Where R1.Workorder_Guid = '" + old_Relation_ID + "' And R2.Workorder_Guid Is Not Null", dataSourceID);
					log.debug("查询获取工单的上一步处理人 SQL:" + getOldSelectSQL + " 结果数量:" + getOldSelectSQL.size());

					//获取当前工单编号对应的CRM上一步处理信息
					String before_Handlegroup_ID = null;
					String before_Handleby_ID = null;
					String before_From_Stepidmap = null;

					if(getOldSelectSQL.size() > 0) {
						before_Handlegroup_ID = getOldSelectSQL.get(0).get("Handlegroup");
						before_Handleby_ID = getOldSelectSQL.get(0).get("Handleby");
						before_From_Stepidmap = getOldSelectSQL.get(0).get("From_Stepidmap");
					}else{
						before_Handlegroup_ID = null;
						before_Handleby_ID = null;
						before_From_Stepidmap = null;
					}
					log.debug("提前获取工单的上一步处理人-结果before_Handlegroup_ID:" + before_Handlegroup_ID + " before_Handleby_ID:" + before_Handleby_ID + " before_From_Stepidmap:" + before_From_Stepidmap);

					// 查询下一个工单回访处理人
					def getRevSelectSQL = eliteBean.chaxun_sql("select Top 1 Rec_Guid, Rolegroup_ID, Staff_ID From SZWG_WO_RevisitStaff sr LEFT JOIN Group_Area ga on sr.AREA=ga.Area_guid Where Del_Flag = '0' and area_id='" + return_area + "'  Order By Revisit_Num", dataSourceID);
					log.debug("查询下一个工单回访处理人 SQL:" + getRevSelectSQL + " 结果数量:" + getRevSelectSQL.size());

					//获取查询下一个工单回访处理人
					String rev_Rec_Guid = null;
					String rev_Handlegroup_ID = null;
					String rev_Handleby_ID = null;

					if(getRevSelectSQL.size() > 0) {
						rev_Rec_Guid = getRevSelectSQL.get(0).get("Rec_Guid");
						rev_Handlegroup_ID = getRevSelectSQL.get(0).get("Rolegroup_ID");
						rev_Handleby_ID = getRevSelectSQL.get(0).get("Staff_ID");
					}else{
						rev_Rec_Guid = "";
						rev_Handlegroup_ID = "";
						rev_Handleby_ID = "";
					}
					log.debug("查询下一个工单回访处理人-结果rev_Rec_Guid:" + rev_Rec_Guid + " rev_Handlegroup_ID:" + rev_Handlegroup_ID + " rev_Handleby_ID:" + rev_Handleby_ID);
					//此工单存在，且为ZUOC2L处理节点时候才能改变工单状态，或者回访节点
					log.debug("工单当前步骤节点为:" + old_Curstep_Id );
					if(old_Relation_ID != null && old_Relation_ID.length() > 0 && ((old_Curstep_Id=='BOVH1J'&&(workorderstatus == "1"||workorderstatus == "2"||workorderstatus == "6"))||(old_Curstep_Id=='ZUOC2L'&&(workorderstatus == "2"||workorderstatus == "3"||workorderstatus == "4"||workorderstatus == "5"||workorderstatus == "7"||workorderstatus == "9"))||(old_Curstep_Id=='GSN988'&&(workorderstatus == "1"||workorderstatus == "2"||workorderstatus == "3"||workorderstatus == "4"||workorderstatus == "6")||(old_Curstep_Id=='0DKTUC'&&(workorderstatus == "2"||workorderstatus == "3"||workorderstatus == "5"||workorderstatus == "6"||workorderstatus == "7"))))){
						try {
							//更新工单处理结果
							String[] data=new String[2];
							data[0]  = "Insert Into Tabcswgdmerge_His Select Newid(),* From Tabcswgdmerge Where Relation_Id = '" + old_Relation_ID + "'";
							//data[1]  = "Update Tabcswgdmerge Set Workorderstatus = '" + workorderstatus + "', Modifyby = '" + modifyby + "', Modifyname = '" + modifyname + "', Modifydate = '" + modifydate + "', Handlingopinions = '" + handlingopinions + "', Stepnode = '" + stepnode + "', Timelimit = '" + timelimit + "' Where Relation_Id = '" + old_Relation_ID + "'";
							data[1]  = "Update Tabcswgdmerge Set Workorderstatus = '" + workorderstatus + "', Modifyby = '" + modifyby + "', Modifyname = '" + modifyname + "', Modifydate = '" + modifydate + "', Handlingopinions = '" + handlingopinions + "' Where Relation_Id = '" + old_Relation_ID + "'";
							log.debug("更新工单处理结果SQL:" + data.toString());
							int[] dataresult = eliteBean.updateBatch(data, dataSourceID);
							log.debug("更新工单处理结果SQL结果:" + dataresult.toString());

							//判断处理工单状态进行下一步处理
							// 1-派单 UCIS主动调用接口通知当前工单已经派单 CRM处理规则是自动将工单默认下送到处理状态不在需要坐席处理
							if (workorderstatus == "1") {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', 'SELITE', 'CW0001', 'SENDNS', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', 'ZUOC2L', Getdate(), Getdate())";
								data1[1] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'CW0001', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_Curstep_Id+"', 'ZUOC2L')";
								data1[2] = "Update Objective Set Handlegroup_Id = 'CW0001', Handleby_Id = 'SELITE', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = 'ZUOC2L', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = 'CW0001', Handleby_Id = 'SELITE', Map_Id = Map_Id, Curstepstatus = Curstepstatus, Curstep_Id = 'ZUOC2L', Curstep_Id_Map = 'ZUOC2L', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());

							}

							// 2-催单 CRM处理规则更新工单为催单状态
							if (workorderstatus == "2") {
								String[] data1=new String[1];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', Newid(), 'SELITE', 'CW0001', 'URGE ', '"+handlingopinions+"', Getdate(), '"+old_Curstep_Id+"', Getdate(), Getdate())";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 3-回退 将工单回退到上一步工单处理人 处理步骤回退-派单节点-接单坐席
							if (workorderstatus == "3"&&(old_Curstep_Id=='ZUOC2L'||old_Curstep_Id=='GSN988')) {
								String[] data1=new String[5];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + before_Handleby_ID + "', '" + before_Handlegroup_ID + "', 'BACK', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '" + before_From_Stepidmap + "', Getdate(), Getdate())";
								data1[1] = "Delete From Workorderpath3 Where Workorder_Guid = '" + old_Relation_ID + "' And From_Stepidmap = '" + before_From_Stepidmap + "' And To_stepidmap = '" + old_Curstep_Id + "'";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '" + before_From_Stepidmap + "', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Map_Id = Map_Id, Curstep_Id = '" + before_From_Stepidmap + "', Curstep_Id_Map = '" + before_From_Stepidmap + "', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[4] = "INSERT INTO hs_spring_screen (noticeId, noticeGroup, noticeBy, createGroup, createBy, noticeContent, createddate, limitCount, loadCount, noticeType, objective_guid) VALUES(NEWID(), '"+old_Handlegroup_ID+"', '"+old_Handleby_ID+"', 'SYSTEM', 'SELITE','工单编号：【"+workorderid+"】已退回，请及时处理！ 原因：【"+handlingopinions+"】' ,GETDATE(), 1, 0, '退单弹窗', '"+old_Relation_ID+"')";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}
							// 3-回退 将工单回退到上一步工单处理人 回访步骤回退-处理节点-处理坐席
							if (workorderstatus == "3"&&old_Curstep_Id=='0DKTUC') {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + before_Handleby_ID + "', '" + before_Handlegroup_ID + "', 'BACK', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', 'ZUOC2L', Getdate(), Getdate())";
								data1[1] = "Delete From Workorderpath3 Where Workorder_Guid = '" + old_Relation_ID + "' And From_Stepidmap = 'ZUOC2L' And To_stepidmap = '" + old_Curstep_Id + "'";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = 'ZUOC2L', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Map_Id = Map_Id, Curstep_Id = 'ZUOC2L', Curstep_Id_Map = 'ZUOC2L', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 4-待回访 将工单下送到回访节点
							if (workorderstatus == "4") {
								String[] data1=new String[8];
								data1[0]  = "insert into TABCswgdmerge_His select NEWID(), * from TABCswgdmerge where Relation_ID = '"+old_Relation_ID+"'";
								data1[1]  = "update TABCswgdmerge set handlebusiness1='"+handlebusiness1+"',handlebusiness2='"+handlebusiness2+"',handlebusiness3='"+handlebusiness3+"',leakagecondition='"+leakagecondition+"',handledescription='"+handledescription+"',remarks='"+remarks+"',failurecause='"+failurecause+"',starttime='"+starttime+"',finishtime='"+finishtime+"',workingday='"+workingday+"',handlerby='"+handlerby+"',handlerphone='"+handlerphone+"',responsibleperson='"+responsibleperson+"',arriveddate='"+arriveddate+"',firstarrivaldate='"+firstarrivaldate+"',checkgrade='"+checkgrade+"',registercode='"+registercode+"',requestdate='"+requestdate+"',handlername='"+handlername+"',responsiblename='"+responsiblename+"',registercodename='"+registercodename+"',doordate='"+doordate+"' where Relation_ID = '"+old_Relation_ID+"'";
								data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + rev_Handleby_ID + "', '" + rev_Handlegroup_ID + "', 'SENDNS', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '0DKTUC', Getdate(), Getdate())";
								data1[3] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'CW0001', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_Curstep_Id+"', '0DKTUC')";
								data1[4] = "Update Objective Set Handlegroup_Id = '" + rev_Handlegroup_ID + "', Handleby_Id = '" + rev_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '0DKTUC', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[5] = "Update Elitetask Set Handlegroup_Id = '" + rev_Handlegroup_ID + "', Handleby_Id = '" + rev_Handleby_ID + "', Map_Id = Map_Id, Curstepstatus = Curstepstatus, Curstep_Id = '0DKTUC', Curstep_Id_Map = '0DKTUC', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[6] = "Insert Into Szwg_Wo_Revisitstaff_List (List_Guid, Rec_Guid, Workorder_Id, Rolegroup_Id, Staff_Id, Createdtime) Values (NewID(), '" + rev_Rec_Guid + "', '" + id + "', '" + rev_Handlegroup_ID + "', '" + rev_Handleby_ID + "', GetDate())";
								data1[7] = "Update SZWG_WO_RevisitStaff Set Revisit_Num = IsNull(Revisit_Num, 0) + 1 Where Rec_Guid = '" + rev_Rec_Guid + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 5-回访完成 将工单推送到结案状态，并更新回访结果--暂定通过回访接口送回访结果
							if (workorderstatus == "5") {
								String[] data1=new String[6];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', 'SELITE', 'CW0001', 'END', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '0DKTUC', Getdate(), Getdate())";
								data1[1] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'CW0001', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_Curstep_Id+"', '0DKTUC')";
								data1[2] = "Update Objective Set Handlegroup_Id = 'CW0001', Handleby_Id = 'SELITE', Objectivestatus = 'CLOSE', Map_Id = Map_Id, Curstep_Id_Map = '0DKTUC', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = 'CW0001', Handleby_Id = 'SELITE', Map_Id = Map_Id, Curstepstatus = 'END', Curstep_Id = '0DKTUC', Curstep_Id_Map = '0DKTUC', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[4] = "Insert Into Tabcswgdmerge_His Select Newid(),* From Tabcswgdmerge Where Relation_Id = '" + old_Relation_ID + "'";
								data1[5] = "update TABCswgdmerge set userreflect='" + userreflect + "',serviceattitude='" + serviceattitude + "',quality='" + quality + "',returnpeople='" + returnpeople + "',returnname='" + returnname + "',feedbacktime='" + feedbacktime + "',satisfaction='" + satisfaction + "',empattire='" + empattire + "',feedback='" + feedback + "' where  Relation_ID = '" + old_Relation_ID + "'";

								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
								
								// 获取工单的业务来源-线上平台与数字化城管的单不需要回访，直接结案；需更新处理步骤的数据
								def getYwlySelectSQL = eliteBean.chaxun_sql("select operationsource as servicesource,rangecodearea as rangecodearea from TABCswgdmerge where Relation_ID = '"+old_Relation_ID+"'", dataSourceID);
								log.debug("查询获取业务来源sql:" + getYwlySelectSQL);
								
								String servicesource = null;
								String rangecodearea = null;
								servicesource = getYwlySelectSQL.get(0).get("servicesource");
								rangecodearea = getYwlySelectSQL.get(0).get("rangecodearea");
								log.debug("查询获取业务来源是：" + servicesource+",区域："+rangecodearea);
								//15 线上平台-自助终端  16 线上平台-微信公众号  17 线上平台-网厅  18 线上平台-i深圳   4 数字化城管网站
								if(rangecodearea=='JT'&&(servicesource=="4"||servicesource=="15"||servicesource=="16"||servicesource=="17"||servicesource=="18")) {
									String[] data2=new String[2];
									data2[0]  = "insert into TABCswgdmerge_His select NEWID(), * from TABCswgdmerge where Relation_ID = '"+old_Relation_ID+"'";
									data2[1]  = "update TABCswgdmerge set handlebusiness1='"+handlebusiness1+"',handlebusiness2='"+handlebusiness2+"',handlebusiness3='"+handlebusiness3+"',leakagecondition='"+leakagecondition+"',handledescription='"+handledescription+"',remarks='"+remarks+"',failurecause='"+failurecause+"',starttime='"+starttime+"',finishtime='"+finishtime+"',workingday='"+workingday+"',handlerby='"+handlerby+"',handlerphone='"+handlerphone+"',responsibleperson='"+responsibleperson+"',arriveddate='"+arriveddate+"',firstarrivaldate='"+firstarrivaldate+"',checkgrade='"+checkgrade+"',registercode='"+registercode+"',requestdate='"+requestdate+"',handlername='"+handlername+"',responsiblename='"+responsiblename+"',registercodename='"+registercodename+"',doordate='"+doordate+"' where Relation_ID = '"+old_Relation_ID+"'";
									log.debug("判断工单数据更新进行下一步处理SQL:" + data2.toString());
									int[] dataresult2 = eliteBean.updateBatch(data2, dataSourceID);
									log.debug("判断工单数据更新进行下一步处理SQL结果:" + dataresult2.toString());
								}
							}

							// 6-废弃/失败 直接将工单失败结案
							if (workorderstatus == "6") {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', 'SELITE', 'CW0001', 'FAIL', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '0DKTUC', Getdate(), Getdate())";
								data1[1] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'CW0001', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_Curstep_Id+"', '0DKTUC')";
								data1[2] = "Update Objective Set Handlegroup_Id = 'CW0001', Handleby_Id = 'SELITE', Objectivestatus = 'FAIL', Map_Id = Map_Id, Curstep_Id_Map = '0DKTUC', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = 'CW0001', Handleby_Id = 'SELITE', Map_Id = Map_Id, Curstepstatus = 'FAIL', Curstep_Id = '0DKTUC', Curstep_Id_Map = '0DKTUC', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 7-返工 将工单回退到接单坐席,不确定返工和CRM什么关系，暂时按退回处理
							if (workorderstatus == "7") {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + before_Handleby_ID + "', '" + before_Handlegroup_ID + "', 'BACK', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '" + before_From_Stepidmap + "', Getdate(), Getdate())";
								data1[1] = "Delete From Workorderpath3 Where Workorder_Guid = '" + old_Relation_ID + "' And From_Stepidmap = '" + before_From_Stepidmap + "' And To_stepidmap = '" + old_Curstep_Id + "'";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '" + before_From_Stepidmap + "', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Map_Id = Map_Id,Curstep_Id = '" + before_From_Stepidmap + "', Curstep_Id_Map = '" + before_From_Stepidmap + "', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 8-激活 基于UCIS调用时指定的激活节点将工单重新打开
							// 9-延期申请【实际为退单】 将工单状态变更为延期申请，更新延期申请时间的操作暂定由坐席去修改,CRM只在历史步骤提示处理意见内容
							if (workorderstatus == "9") {
								//String[] data1=new String[1];
								//data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', 'SELITE', 'CW0001', 'OPEN', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '" + old_Curstep_Id + "', Getdate(), Getdate())";
								//data1[1]  = "Update Tabcswgdmerge Set datelimit= DATEADD(HOUR, +"+timelimit+", datelimit)  Where Relation_Id = '" + old_Relation_ID + "'";
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + before_Handleby_ID + "', '" + before_Handlegroup_ID + "', 'BACK', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '" + before_From_Stepidmap + "', Getdate(), Getdate())";
								data1[1] = "Delete From Workorderpath3 Where Workorder_Guid = '" + old_Relation_ID + "' And From_Stepidmap = '" + before_From_Stepidmap + "' And To_stepidmap = '" + old_Curstep_Id + "'";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '" + before_From_Stepidmap + "', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Map_Id = Map_Id, Curstep_Id = '" + before_From_Stepidmap + "', Curstep_Id_Map = '" + before_From_Stepidmap + "', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							dealResult = "1";
						} catch (Exception e) {
							dealResult = "0";
							dealMessage = "当前处理状态变更的工单整体处理SQL异常";
							log.error(dealMessage + ":" + e.getMessage());
						}

					}else {
						dealResult = "0";
						dealMessage = "当前处理结果变更的工单在CRM系统不存在ID或工单步骤不在处理节点";
						log.warn(dealMessage + ":" + id);
					}
				}

				dealMap.put(id, dealResult);
				dealMsgMap.put(id, dealMessage);
			}

			// 将每条处理结果转换为JSONArray
			int j = 0;
			for (Map.Entry<String, String> m : dealMap.entrySet()) {
				JSONObject resObject = new JSONObject();
				resObject.put("id", m.getKey());
				resObject.put("code", m.getValue());
				resObject.put("message", dealMsgMap.get(m.getKey()).toString());
				resArray.put(j, resObject);
				j++;
			}

			log.debug(resArray.toString());

			long endTime = System.currentTimeMillis();
			// 包装拼接返回JSONObject
			reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"处理完成、调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":" + resArray.toString() + "}";

		} catch (JSONException e) {
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
		}

		return reJson;
	}


	// 光明工单处理
	public static String GMHandle(EliteBean eliteBean, String sys_source, String sys_flag, String inJson, String dataSourceID) {
		long startTime = System.currentTimeMillis();
		JSONArray arrJsonArray;
		Map<String, String> dealMap = new HashMap<String, String>();
		Map<String, String> dealMsgMap = new HashMap<String, String>();
		JSONArray resArray = new JSONArray();
		String reJson = null;
		try {
			arrJsonArray = new JSONArray(inJson);

			int arrInt = arrJsonArray.length();
			log.debug("接入List数量:" + arrInt);

			for (int i = 0; i < arrInt; i++) {

				log.debug("当前处理数组位置:" + (i+1));
				JSONObject arrCustJson = arrJsonArray.getJSONObject(i);

				//解析接口推送的处理结果信息
				String workorderstatus = GetJV(arrCustJson, "workorderstatus");
				String modifyby = GetJV(arrCustJson, "modifyby");
				String modifyname = GetJV(arrCustJson, "modifyname");
				String modifydate = GetJV(arrCustJson, "modifydate");
				String id = GetJV(arrCustJson, "id");
				String handlebusiness1 = GetJV(arrCustJson, "handlebusiness1");
				String handlebusiness2 = GetJV(arrCustJson, "handlebusiness2");
				String handlebusiness3 = GetJV(arrCustJson, "handlebusiness3");
				String leakagecondition = GetJV(arrCustJson, "leakagecondition");
				String handledescription = GetJV(arrCustJson, "handledescription");
				String remarks = GetJV(arrCustJson, "remarks");
				String failurecause = GetJV(arrCustJson, "failurecause");
				String starttime = GetJV(arrCustJson, "starttime");
				String finishtime = GetJV(arrCustJson, "finishtime");
				String workingday = GetJV(arrCustJson, "workingday");
				String handlerby = GetJV(arrCustJson, "handlerby");
				String handlerphone = GetJV(arrCustJson, "handlerphone");
				String responsibleperson = GetJV(arrCustJson, "responsibleperson");
				String arriveddate = GetJV(arrCustJson, "arriveddate");
				String firstarrivaldate = GetJV(arrCustJson, "firstarrivaldate");
				String checkgrade = GetJV(arrCustJson, "checkgrade");
				String registercode = GetJV(arrCustJson, "registercode");
				String requestdate = GetJV(arrCustJson, "requestdate");
				String doordate = GetJV(arrCustJson, "doordate");
				//状态
				String handlingopinions = GetJV(arrCustJson, "handlingopinions");
				String stepnode = GetJV(arrCustJson, "stepnode");
				String timelimit = GetJV(arrCustJson, "timelimit");

				String dealResult = "";
				String dealMessage = "";

				//处理步骤校验
				if(sys_flag=='4'&&(isNull(modifyby)||isNull(modifyname)||isNull(modifydate)||isNull(id)||isNull(starttime)||isNull(finishtime)))
				{
					dealResult = "0";
					dealMessage = isNullMSG("modifyby", modifyby) + isNullMSG("modifyname", modifyname) + isNullMSG("modifydate", modifydate) + isNullMSG("id", id) + isNullMSG("starttime", starttime) + isNullMSG("finishtime", finishtime);
					log.warn("调用参数非空校验异常:" + dealMessage);

				}
				//除处理步骤，其他状态的校验
				if(sys_flag!='4'&&(isNull(modifyby)||isNull(modifyname)||isNull(modifydate)||isNull(id)))
				{
					dealResult = "0";
					dealMessage = isNullMSG("modifyby", modifyby) + isNullMSG("modifyname", modifyname) + isNullMSG("modifydate", modifydate) + isNullMSG("id", id);
					log.warn("调用参数非空校验异常:" + dealMessage);

				}else {
					//查询现有当前工单编号是否存在现有工单
					def getSelectSQL = eliteBean.chaxun_sql("Select R1.Relation_ID, R3.Handlegroup_ID, R3.Handleby_ID, R3.CurstepStatus, R2.curstep_id_map as Curstep_Id ,R1.receiver as return_area,R2.workorderid as workorderid From Tabcswgdmerge R1 Left Join Objective R2 On R1.Relation_Id = R2.Objective_Guid Left Join Elitetask R3 On R1.Relation_Id = R3.Objective_Guid Where R1.workorderid = '" + id + "'", dataSourceID);
					log.debug("查询当前同步的工单是否已经存在 SQL:" + getSelectSQL + " 结果数量:" + getSelectSQL.size());

					//获取当前工单编号对应的CRM唯一工单Guid Relation_ID
					String old_Relation_ID = null;
					String old_Handlegroup_ID = null;
					String old_Handleby_ID = null;
					String old_CurstepStatus = null;
					String old_Curstep_Id = null;
					String workorderid = null;
					//回访人员区域标识
					String return_area = null;

					if(getSelectSQL.size() > 0) {
						old_Relation_ID = getSelectSQL.get(0).get("Relation_ID");
						old_Handlegroup_ID = getSelectSQL.get(0).get("Handlegroup_ID");
						old_Handleby_ID = getSelectSQL.get(0).get("Handleby_ID");
						old_CurstepStatus = getSelectSQL.get(0).get("CurstepStatus");
						old_Curstep_Id = getSelectSQL.get(0).get("Curstep_Id");
						workorderid = getSelectSQL.get(0).get("workorderid");
						return_area = getSelectSQL.get(0).get("return_area");
					}else{
						old_Relation_ID = "";
						old_Handlegroup_ID = "";
						old_Handleby_ID = "";
						old_CurstepStatus = "";
						old_Curstep_Id = "";
						workorderid = "";
						return_area = "";
					}
					log.debug("查询现有当前工单编号是否存在现有工单-结果old_Relation_ID:" + old_Relation_ID + " old_Handlegroup_ID:" + old_Handlegroup_ID + " old_Handleby_ID:" + old_Handleby_ID + " old_CurstepStatus:" + old_CurstepStatus + " old_Curstep_Id:" + old_Curstep_Id+ " workorderid:" + workorderid);

					// 提前获取工单的上一步处理人
					def getOldSelectSQL = eliteBean.chaxun_sql("Select R1.Handlegroup, R1.Handleby, R1.From_Stepidmap From Workorderpath3 R1 Left Join (Select Workorder_Guid, Max(Modifieddate) Modifieddate From Workorderpath3 Where To_Stepidmap = 'HWCU5I' And Workorder_Guid = '" + old_Relation_ID + "' Group By Workorder_Guid) R2 On R1.Workorder_Guid = R2.Workorder_Guid And R1.Modifieddate = R2.Modifieddate Where R1.Workorder_Guid = '" + old_Relation_ID + "' And R2.Workorder_Guid Is Not Null", dataSourceID);
					log.debug("查询获取工单的上一步处理人 SQL:" + getOldSelectSQL + " 结果数量:" + getOldSelectSQL.size());

					//获取当前工单编号对应的CRM上一步处理信息
					String before_Handlegroup_ID = null;
					String before_Handleby_ID = null;
					String before_From_Stepidmap = null;

					if(getOldSelectSQL.size() > 0) {
						before_Handlegroup_ID = getOldSelectSQL.get(0).get("Handlegroup");
						before_Handleby_ID = getOldSelectSQL.get(0).get("Handleby");
						before_From_Stepidmap = getOldSelectSQL.get(0).get("From_Stepidmap");
					}else{
						before_Handlegroup_ID = null;
						before_Handleby_ID = null;
						before_From_Stepidmap = null;
					}
					log.debug("提前获取工单的上一步处理人-结果before_Handlegroup_ID:" + before_Handlegroup_ID + " before_Handleby_ID:" + before_Handleby_ID + " before_From_Stepidmap:" + before_From_Stepidmap);

					// 查询下一个工单回访处理人
					def getRevSelectSQL = eliteBean.chaxun_sql("select Top 1 Rec_Guid, Rolegroup_ID, Staff_ID From SZWG_WO_RevisitStaff sr LEFT JOIN Group_Area ga on sr.AREA=ga.Area_guid Where Del_Flag = '0' and area_id='" + return_area + "'  Order By Revisit_Num", dataSourceID);
					log.debug("查询下一个工单回访处理人 SQL:" + getRevSelectSQL + " 结果数量:" + getRevSelectSQL.size());
	
					//获取查询下一个工单回访处理人
					String rev_Rec_Guid = null;
					String rev_Handlegroup_ID = null;
					String rev_Handleby_ID = null;
	
					if(getRevSelectSQL.size() > 0) {
						rev_Rec_Guid = getRevSelectSQL.get(0).get("Rec_Guid");
						rev_Handlegroup_ID = getRevSelectSQL.get(0).get("Rolegroup_ID");
						rev_Handleby_ID = getRevSelectSQL.get(0).get("Staff_ID");
					}else{
						rev_Rec_Guid = "";
						rev_Handlegroup_ID = "";
						rev_Handleby_ID = "";
					}
					//此工单存在，且为HWCU5I处理节点时候才能改变工单状态
					log.debug("工单当前步骤节点为:" + old_Curstep_Id );
					if(old_Relation_ID != null && old_Relation_ID.length() > 0 && old_Curstep_Id=='HWCU5I') {
						try {
							//更新工单处理结果
							String[] data=new String[2];
							data[0]  = "Insert Into Tabcswgdmerge_His Select Newid(),* From Tabcswgdmerge Where Relation_Id = '" + old_Relation_ID + "'";
							//data[1]  = "Update Tabcswgdmerge Set Workorderstatus = '" + workorderstatus + "', Modifyby = '" + modifyby + "', Modifyname = '" + modifyname + "', Modifydate = '" + modifydate + "', Handlingopinions = '" + handlingopinions + "', Stepnode = '" + stepnode + "', Timelimit = '" + timelimit + "' Where Relation_Id = '" + old_Relation_ID + "'";
							data[1]  = "Update Tabcswgdmerge Set Workorderstatus = '" + workorderstatus + "', Modifyby = '" + modifyby + "', Modifyname = '" + modifyname + "', Modifydate = '" + modifydate + "', Handlingopinions = '" + handlingopinions + "' Where Relation_Id = '" + old_Relation_ID + "'";
							log.debug("更新工单处理结果SQL:" + data.toString());
							int[] dataresult = eliteBean.updateBatch(data, dataSourceID);
							log.debug("更新工单处理结果SQL结果:" + dataresult.toString());

							//判断处理工单状态进行下一步处理

							// 2-催单 CRM处理规则更新工单为催单状态
							if (workorderstatus == "2") {
								String[] data1=new String[1];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', Newid(), 'SELITE', 'CW0001', 'URGE ', '"+handlingopinions+"', Getdate(), '"+old_Curstep_Id+"', Getdate(), Getdate())";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 3-回退 将工单回退到上一步工单处理人一般是接单坐席
							if (workorderstatus == "3") {
								String[] data1=new String[5];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + before_Handleby_ID + "', '" + before_Handlegroup_ID + "', 'BACK  ', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '" + before_From_Stepidmap + "', Getdate(), Getdate())";
								data1[1] = "Delete From Workorderpath3 Where Workorder_Guid = '" + old_Relation_ID + "' And From_Stepidmap = '" + before_From_Stepidmap + "' And To_stepidmap = '" + old_Curstep_Id + "'";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '" + before_From_Stepidmap + "', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Map_Id = Map_Id, Curstep_Id = '" + before_From_Stepidmap + "', Curstep_Id_Map = '" + before_From_Stepidmap + "', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[4] = "INSERT INTO hs_spring_screen (noticeId, noticeGroup, noticeBy, createGroup, createBy, noticeContent, createddate, limitCount, loadCount, noticeType, objective_guid) VALUES(NEWID(), '"+old_Handlegroup_ID+"', '"+old_Handleby_ID+"', 'SYSTEM', 'SELITE','工单编号：【"+workorderid+"】已退回，请及时处理！ 原因：【"+handlingopinions+"】' ,GETDATE(), 1, 0, '退单弹窗', '"+old_Relation_ID+"')";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 4-待回访 将工单下送到回访节点
							if (workorderstatus == "4") {
								def handlebusiness3result = eliteBean.chaxun_sql("select Code_ID as Code_ID from C_CodeTables where use_type='三级业务类型' and Code_Name='"+handlebusiness3+"'", dataSourceID);
								if(handlebusiness3result.size()>0){
									handlebusiness3 = handlebusiness3result.get(0).get("Code_ID");
									def handlebusiness1result = eliteBean.chaxun_sql("select PCode_ID as PCode_ID,Code_ID as Code_ID from C_CodeTables where use_type='二级业务类型' and Code_Name='"+handlebusiness2+"'", dataSourceID);
									if(handlebusiness1result.size()>0){
										handlebusiness1 = handlebusiness1result.get(0).get("PCode_ID");
										handlebusiness2 = handlebusiness3result.get(0).get("Code_ID");
										log.debug("光明业务类型:" + handlebusiness1+","+handlebusiness2+","+handlebusiness3);
									}
								}
								String[] data1=new String[7];
								data1[0] = "update TABCswgdmerge set handlebusiness1='"+handlebusiness1+"',handlebusiness2='"+handlebusiness2+"',handlebusiness3='"+handlebusiness3+"',leakagecondition='"+leakagecondition+"',handledescription='"+handledescription+"',remarks='"+remarks+"',failurecause='"+failurecause+"',starttime='"+starttime+"',finishtime='"+finishtime+"',workingday='"+workingday+"',handlerby='"+handlerby+"',handlerphone='"+handlerphone+"',responsibleperson='"+responsibleperson+"',arriveddate='"+arriveddate+"',firstarrivaldate='"+firstarrivaldate+"',checkgrade='"+checkgrade+"',registercode='"+registercode+"',requestdate='"+requestdate+"',doordate='"+doordate+"' where Relation_ID = '"+old_Relation_ID+"'";
								data1[1] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + rev_Handleby_ID + "', '" + rev_Handlegroup_ID + "', 'SENDNS', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '0DKTUC', Getdate(), Getdate())";
								data1[2] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'CW0001', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_Curstep_Id+"', '0DKTUC')";
								data1[3] = "Update Objective Set Handlegroup_Id = '" + rev_Handlegroup_ID + "', Handleby_Id = '" + rev_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '0DKTUC', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[4] = "Update Elitetask Set Handlegroup_Id = '" + rev_Handlegroup_ID + "', Handleby_Id = '" + rev_Handleby_ID + "', Map_Id = Map_Id, Curstepstatus = Curstepstatus, Curstep_Id = '0DKTUC', Curstep_Id_Map = '0DKTUC', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[5] = "Insert Into Szwg_Wo_Revisitstaff_List (List_Guid, Rec_Guid, Workorder_Id, Rolegroup_Id, Staff_Id, Createdtime) Values (NewID(), '" + rev_Rec_Guid + "', '" + id + "', '" + rev_Handlegroup_ID + "', '" + rev_Handleby_ID + "', GetDate())";
								data1[6] = "Update SZWG_WO_RevisitStaff Set Revisit_Num = IsNull(Revisit_Num, 0) + 1 Where Rec_Guid = '" + rev_Rec_Guid + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}
							// 9-延期申请
							if (workorderstatus == "9") {
								String[] data1=new String[1];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + old_Handleby_ID + "', '" + old_Handlegroup_ID + "', 'DELAY ', '"+timelimit+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '" + old_Curstep_Id + "', Getdate(), Getdate())";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}
							// 14-销单申请
							/*if (workorderstatus == "14") {
								String[] data1=new String[1];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + old_Handleby_ID + "', '" + old_Handlegroup_ID + "', 'VISIT ', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+before_From_Stepidmap+"', '" + old_Curstep_Id + "', Getdate(), Getdate())";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}*/
							dealResult = "1";
						} catch (Exception e) {
							dealResult = "0";
							dealMessage = "当前处理状态变更的工单整体处理SQL异常";
							log.error(dealMessage + ":" + e.getMessage());
						}

					}else {
						dealResult = "0";
						dealMessage = "当前处理结果变更的工单在CRM系统不存在ID或工单步骤不在处理节点";
						log.warn(dealMessage + ":" + id);
					}
				}

				dealMap.put(id, dealResult);
				dealMsgMap.put(id, dealMessage);
			}

			// 将每条处理结果转换为JSONArray
			int j = 0;
			for (Map.Entry<String, String> m : dealMap.entrySet()) {
				JSONObject resObject = new JSONObject();
				resObject.put("id", m.getKey());
				resObject.put("code", m.getValue());
				resObject.put("message", dealMsgMap.get(m.getKey()).toString());
				resArray.put(j, resObject);
				j++;
			}

			log.debug(resArray.toString());

			long endTime = System.currentTimeMillis();
			// 包装拼接返回JSONObject
			reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"处理完成、调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":" + resArray.toString() + "}";

		} catch (JSONException e) {
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
		}

		return reJson;
	
	}
	
	// 宝安工单处理
	public static String BAHandle(EliteBean eliteBean, String sys_source, String sys_flag, String inJson, String dataSourceID) {
		long startTime = System.currentTimeMillis();
		JSONArray arrJsonArray;
		Map<String, String> dealMap = new HashMap<String, String>();
		Map<String, String> dealMsgMap = new HashMap<String, String>();
		JSONArray resArray = new JSONArray();
		String reJson = null;
		try {
			arrJsonArray = new JSONArray(inJson);

			int arrInt = arrJsonArray.length();
			log.debug("接入List数量:" + arrInt);

			for (int i = 0; i < arrInt; i++) {

				log.debug("当前处理数组位置:" + (i+1));
				JSONObject arrCustJson = arrJsonArray.getJSONObject(i);

				//解析接口推送的处理结果信息
				String workorderstatus = GetJV(arrCustJson, "workorderstatus");
				String modifyby = GetJV(arrCustJson, "modifyby");
				String modifyname = GetJV(arrCustJson, "modifyname");
				String modifydate = GetJV(arrCustJson, "modifydate");
				String id = GetJV(arrCustJson, "id");
				String handlebusiness1 = GetJV(arrCustJson, "handlebusiness1");
				String handlebusiness2 = GetJV(arrCustJson, "handlebusiness2");
				String handlebusiness3 = GetJV(arrCustJson, "handlebusiness3");
				String leakagecondition = GetJV(arrCustJson, "leakagecondition");
				String handledescription = GetJV(arrCustJson, "handledescription");
				String remarks = GetJV(arrCustJson, "remarks");
				String failurecause = GetJV(arrCustJson, "failurecause");
				String starttime = GetJV(arrCustJson, "starttime");
				String finishtime = GetJV(arrCustJson, "finishtime");
				String workingday = GetJV(arrCustJson, "workingday");
				String handlerby = GetJV(arrCustJson, "handlerby");
				String handlerphone = GetJV(arrCustJson, "handlerphone");
				String responsibleperson = GetJV(arrCustJson, "responsibleperson");
				String arriveddate = GetJV(arrCustJson, "arriveddate");
				String firstarrivaldate = GetJV(arrCustJson, "firstarrivaldate");
				String checkgrade = GetJV(arrCustJson, "checkgrade");
				String registercode = GetJV(arrCustJson, "registercode");
				String requestdate = GetJV(arrCustJson, "requestdate");
				String doordate = GetJV(arrCustJson, "doordate");
				//状态
				String handlingopinions = GetJV(arrCustJson, "handlingopinions");
				String stepnode = GetJV(arrCustJson, "stepnode");
				String timelimit = GetJV(arrCustJson, "timelimit");

				String dealResult = "";
				String dealMessage = "";

				//处理步骤校验
				if(sys_flag=='4'&&(isNull(modifyby)||isNull(modifyname)||isNull(modifydate)||isNull(id)||isNull(starttime)||isNull(finishtime)))
				{
					dealResult = "0";
					dealMessage = isNullMSG("modifyby", modifyby) + isNullMSG("modifyname", modifyname) + isNullMSG("modifydate", modifydate) + isNullMSG("id", id) + isNullMSG("starttime", starttime) + isNullMSG("finishtime", finishtime);
					log.warn("调用参数非空校验异常:" + dealMessage);

				}
				//除处理步骤，其他状态的校验
				if(sys_flag!='4'&&(isNull(modifyby)||isNull(modifyname)||isNull(modifydate)||isNull(id)))
				{
					dealResult = "0";
					dealMessage = isNullMSG("modifyby", modifyby) + isNullMSG("modifyname", modifyname) + isNullMSG("modifydate", modifydate) + isNullMSG("id", id);
					log.warn("调用参数非空校验异常:" + dealMessage);

				}else {
					//查询现有当前工单编号是否存在现有工单
					def getSelectSQL = eliteBean.chaxun_sql("Select R1.Relation_ID, R3.Handlegroup_ID, R3.Handleby_ID, R3.CurstepStatus, R2.curstep_id_map as Curstep_Id ,R1.receiver as return_area,R2.workorderid as workorderid From Tabcswgdmerge R1 Left Join Objective R2 On R1.Relation_Id = R2.Objective_Guid Left Join Elitetask R3 On R1.Relation_Id = R3.Objective_Guid Where R1.workorderid = '" + id + "'", dataSourceID);
					log.debug("查询当前同步的工单是否已经存在 SQL:" + getSelectSQL + " 结果数量:" + getSelectSQL.size());

					//获取当前工单编号对应的CRM唯一工单Guid Relation_ID
					String old_Relation_ID = null;
					String old_Handlegroup_ID = null;
					String old_Handleby_ID = null;
					String old_CurstepStatus = null;
					String old_Curstep_Id = null;
					String workorderid = null;
					//回访人员区域标识
					String return_area = null;

					if(getSelectSQL.size() > 0) {
						old_Relation_ID = getSelectSQL.get(0).get("Relation_ID");
						old_Handlegroup_ID = getSelectSQL.get(0).get("Handlegroup_ID");
						old_Handleby_ID = getSelectSQL.get(0).get("Handleby_ID");
						old_CurstepStatus = getSelectSQL.get(0).get("CurstepStatus");
						old_Curstep_Id = getSelectSQL.get(0).get("Curstep_Id");
						workorderid = getSelectSQL.get(0).get("workorderid");
						return_area = getSelectSQL.get(0).get("return_area");
					}else{
						old_Relation_ID = "";
						old_Handlegroup_ID = "";
						old_Handleby_ID = "";
						old_CurstepStatus = "";
						old_Curstep_Id = "";
						workorderid = "";
						return_area = "";
					}
					log.debug("查询现有当前工单编号是否存在现有工单-结果old_Relation_ID:" + old_Relation_ID + " old_Handlegroup_ID:" + old_Handlegroup_ID + " old_Handleby_ID:" + old_Handleby_ID + " old_CurstepStatus:" + old_CurstepStatus + " old_Curstep_Id:" + old_Curstep_Id+ " workorderid:" + workorderid);

					// 提前获取工单的上一步处理人
					def getOldSelectSQL = eliteBean.chaxun_sql("Select R1.Handlegroup, R1.Handleby, R1.From_Stepidmap From Workorderpath3 R1 Left Join (Select Workorder_Guid, Max(Modifieddate) Modifieddate From Workorderpath3 Where To_Stepidmap = 'VXGUM0' And Workorder_Guid = '" + old_Relation_ID + "' Group By Workorder_Guid) R2 On R1.Workorder_Guid = R2.Workorder_Guid And R1.Modifieddate = R2.Modifieddate Where R1.Workorder_Guid = '" + old_Relation_ID + "' And R2.Workorder_Guid Is Not Null", dataSourceID);
					log.debug("查询获取工单的上一步处理人 SQL:" + getOldSelectSQL + " 结果数量:" + getOldSelectSQL.size());

					//获取当前工单编号对应的CRM上一步处理信息
					String before_Handlegroup_ID = null;
					String before_Handleby_ID = null;
					String before_From_Stepidmap = null;

					if(getOldSelectSQL.size() > 0) {
						before_Handlegroup_ID = getOldSelectSQL.get(0).get("Handlegroup");
						before_Handleby_ID = getOldSelectSQL.get(0).get("Handleby");
						before_From_Stepidmap = getOldSelectSQL.get(0).get("From_Stepidmap");
					}else{
						before_Handlegroup_ID = null;
						before_Handleby_ID = null;
						before_From_Stepidmap = null;
					}
					log.debug("提前获取工单的上一步处理人-结果before_Handlegroup_ID:" + before_Handlegroup_ID + " before_Handleby_ID:" + before_Handleby_ID + " before_From_Stepidmap:" + before_From_Stepidmap);

					// 查询下一个工单回访处理人
					def getRevSelectSQL = eliteBean.chaxun_sql("select Top 1 Rec_Guid, Rolegroup_ID, Staff_ID From SZWG_WO_RevisitStaff sr LEFT JOIN Group_Area ga on sr.AREA=ga.Area_guid Where Del_Flag = '0' and area_id='" + return_area + "'  Order By Revisit_Num", dataSourceID);
					log.debug("查询下一个工单回访处理人 SQL:" + getRevSelectSQL + " 结果数量:" + getRevSelectSQL.size());
	
					//获取查询下一个工单回访处理人
					String rev_Rec_Guid = null;
					String rev_Handlegroup_ID = null;
					String rev_Handleby_ID = null;
	
					if(getRevSelectSQL.size() > 0) {
						rev_Rec_Guid = getRevSelectSQL.get(0).get("Rec_Guid");
						rev_Handlegroup_ID = getRevSelectSQL.get(0).get("Rolegroup_ID");
						rev_Handleby_ID = getRevSelectSQL.get(0).get("Staff_ID");
					}else{
						rev_Rec_Guid = "";
						rev_Handlegroup_ID = "";
						rev_Handleby_ID = "";
					}
					//此工单存在，且为VXGUM0处理节点时候才能改变工单状态
					log.debug("工单当前步骤节点为:" + old_Curstep_Id );
					if(old_Relation_ID != null && old_Relation_ID.length() > 0 && old_Curstep_Id=='VXGUM0') {
						try {
							//更新工单处理结果
							String[] data=new String[2];
							data[0]  = "Insert Into Tabcswgdmerge_His Select Newid(),* From Tabcswgdmerge Where Relation_Id = '" + old_Relation_ID + "'";
							data[1]  = "Update Tabcswgdmerge Set Workorderstatus = '" + workorderstatus + "', Modifyby = '" + modifyby + "', Modifyname = '" + modifyname + "', Modifydate = '" + modifydate + "', Handlingopinions = '" + handlingopinions + "' Where Relation_Id = '" + old_Relation_ID + "'";
							log.debug("更新工单处理结果SQL:" + data.toString());
							int[] dataresult = eliteBean.updateBatch(data, dataSourceID);
							log.debug("更新工单处理结果SQL结果:" + dataresult.toString());

							// 3-回退 将工单回退到上一步工单处理人一般是接单坐席
							if (workorderstatus == "3") {
								String[] data1=new String[5];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + before_Handleby_ID + "', '" + before_Handlegroup_ID + "', 'BACK  ', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '" + before_From_Stepidmap + "', Getdate(), Getdate())";
								data1[1] = "Delete From Workorderpath3 Where Workorder_Guid = '" + old_Relation_ID + "' And From_Stepidmap = '" + before_From_Stepidmap + "' And To_stepidmap = '" + old_Curstep_Id + "'";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '" + before_From_Stepidmap + "', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Map_Id = Map_Id, Curstep_Id = '" + before_From_Stepidmap + "', Curstep_Id_Map = '" + before_From_Stepidmap + "', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[4] = "INSERT INTO hs_spring_screen (noticeId, noticeGroup, noticeBy, createGroup, createBy, noticeContent, createddate, limitCount, loadCount, noticeType, objective_guid) VALUES(NEWID(), '"+old_Handlegroup_ID+"', '"+old_Handleby_ID+"', 'SYSTEM', 'SELITE','工单编号：【"+workorderid+"】已退回，请及时处理！ 原因：【"+handlingopinions+"】' ,GETDATE(), 1, 0, '退单弹窗', '"+old_Relation_ID+"')";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 4-待回访 将工单下送到回访节点
							if (workorderstatus == "4") {
								String[] data1=new String[7];
								data1[0] = "update TABCswgdmerge set handlebusiness1='"+handlebusiness1+"',handlebusiness2='"+handlebusiness2+"',handlebusiness3='"+handlebusiness3+"',leakagecondition='"+leakagecondition+"',handledescription='"+handledescription+"',remarks='"+remarks+"',failurecause='"+failurecause+"',starttime='"+starttime+"',finishtime='"+finishtime+"',workingday='"+workingday+"',handlerby='"+handlerby+"',handlerphone='"+handlerphone+"',responsibleperson='"+responsibleperson+"',arriveddate='"+arriveddate+"',firstarrivaldate='"+firstarrivaldate+"',checkgrade='"+checkgrade+"',registercode='"+registercode+"',requestdate='"+requestdate+"',doordate='"+doordate+"' where Relation_ID = '"+old_Relation_ID+"'";
								data1[1] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + rev_Handleby_ID + "', '" + rev_Handlegroup_ID + "', 'SENDNS', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '0DKTUC', Getdate(), Getdate())";
								data1[2] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'CW0001', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_Curstep_Id+"', '0DKTUC')";
								data1[3] = "Update Objective Set Handlegroup_Id = '" + rev_Handlegroup_ID + "', Handleby_Id = '" + rev_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '0DKTUC', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[4] = "Update Elitetask Set Handlegroup_Id = '" + rev_Handlegroup_ID + "', Handleby_Id = '" + rev_Handleby_ID + "', Map_Id = Map_Id, Curstepstatus = Curstepstatus, Curstep_Id = '0DKTUC', Curstep_Id_Map = '0DKTUC', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[5] = "Insert Into Szwg_Wo_Revisitstaff_List (List_Guid, Rec_Guid, Workorder_Id, Rolegroup_Id, Staff_Id, Createdtime) Values (NewID(), '" + rev_Rec_Guid + "', '" + id + "', '" + rev_Handlegroup_ID + "', '" + rev_Handleby_ID + "', GetDate())";
								data1[6] = "Update SZWG_WO_RevisitStaff Set Revisit_Num = IsNull(Revisit_Num, 0) + 1 Where Rec_Guid = '" + rev_Rec_Guid + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}
							// 9-延期申请
							if (workorderstatus == "9") {
								String[] data1=new String[5];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + old_Handleby_ID + "', '" + old_Handlegroup_ID + "', 'DELAY ', '"+timelimit+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_Curstep_Id+"', '" + before_From_Stepidmap + "', Getdate(), Getdate())";
								data1[1] = "Delete From Workorderpath3 Where Workorder_Guid = '" + old_Relation_ID + "' And From_Stepidmap = '" + before_From_Stepidmap + "' And To_stepidmap = '" + old_Curstep_Id + "'";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '" + before_From_Stepidmap + "', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Map_Id = Map_Id, Curstep_Id = '" + before_From_Stepidmap + "', Curstep_Id_Map = '" + before_From_Stepidmap + "', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[4] = "INSERT INTO hs_spring_screen (noticeId, noticeGroup, noticeBy, createGroup, createBy, noticeContent, createddate, limitCount, loadCount, noticeType, objective_guid) VALUES(NEWID(), '"+old_Handlegroup_ID+"', '"+old_Handleby_ID+"', 'SYSTEM', 'SELITE','工单编号：【"+workorderid+"】已退回，请及时处理！ 原因：【"+handlingopinions+"】' ,GETDATE(), 1, 0, '退单弹窗', '"+old_Relation_ID+"')";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}
							dealResult = "1";
						} catch (Exception e) {
							dealResult = "0";
							dealMessage = "当前处理状态变更的工单整体处理SQL异常";
							log.error(dealMessage + ":" + e.getMessage());
						}

					}else {
						dealResult = "0";
						dealMessage = "当前处理结果变更的工单在CRM系统不存在ID或工单步骤不在处理节点";
						log.warn(dealMessage + ":" + id);
					}
				}

				dealMap.put(id, dealResult);
				dealMsgMap.put(id, dealMessage);
			}

			// 将每条处理结果转换为JSONArray
			int j = 0;
			for (Map.Entry<String, String> m : dealMap.entrySet()) {
				JSONObject resObject = new JSONObject();
				resObject.put("id", m.getKey());
				resObject.put("code", m.getValue());
				resObject.put("message", dealMsgMap.get(m.getKey()).toString());
				resArray.put(j, resObject);
				j++;
			}

			log.debug(resArray.toString());

			long endTime = System.currentTimeMillis();
			// 包装拼接返回JSONObject
			reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"处理完成、调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":" + resArray.toString() + "}";

		} catch (JSONException e) {
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
		}

		return reJson;
	
	}

	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		log.debug("解析-k=" + getKey + " v=" + Rev_Value);
		return Rev_Value;
	}

	// 非空校验isNull
	public static boolean isNull(String value) {

		boolean reFlag = true;
		if (value == "" || value.length() <= 0) {
			reFlag = true;
		} else {
			reFlag = false;
		}

		return reFlag;
	}

	// 非空校验isNullMSG
	public static String isNullMSG(String key, String value) {

		String reResult = "";
		if (value == "" || value.length() <= 0) {
			reResult = key + "必填字段;";
		}
		return reResult;
	}

}